package com.simple.datax.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IVwDataxEnterpriseBO;
import com.simple.datax.vo.DataxDictionaryVO;
import com.simple.datax.vo.VwDataxEnterpriseVO;
import com.simple.util.object.PageInfoBean;

@Controller 
@RequestMapping ("/services/dataxFlow") 
public class DataxFlowService extends AbstractDataxBaseService {
	
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;
	@Autowired
	private IVwDataxEnterpriseBO vwDataxEnterpriseBO;
	@RequestMapping (value="/getEnterpriseServiceType", headers="Accept=application/json") 
	@ResponseBody 
	public  Map<String, Object> getEnterpriseServiceType(@RequestParam(value="fiId",required=false) final String fiId,
	   final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		Map<String,Object> map=new HashMap<String, Object>();
		boolean flag=false;
		VwDataxEnterpriseVO vwDataxEnterpriseVO = vwDataxEnterpriseBO.findById(fiId);
		if(vwDataxEnterpriseVO!=null){
			List<DataxDictionaryVO> list= dataxDictionaryBO.findDataxDictionaryVOByEnpterpise(fiId);
			StringBuffer strSelect=new StringBuffer();
			if(list.size()>0){
				flag=true;
				for(int i=0;i<list.size();i++){
					DataxDictionaryVO dataxDictionaryVO =list.get(i);
					strSelect.append("<option value='");
					strSelect.append(dataxDictionaryVO.getId().getFcCode());
					strSelect.append("'>");
					strSelect.append(dataxDictionaryVO.getFcName());
					strSelect.append("</option>");
					
				}
				map.put("vwDataxEnterpriseVO", vwDataxEnterpriseVO);
				map.put("option", strSelect);
			}
		}
		map.put("flag", flag);
		return map;
	}
	@RequestMapping (value="/getEnterpriseCode", headers="Accept=application/json") 
	@ResponseBody 
	public  List<String> getEnterpriseCode(@RequestParam(value="term",required=false) final String term,
	   final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		List<VwDataxEnterpriseVO> listEnterpriseBO = vwDataxEnterpriseBO.findForAutoComplete(term,  new PageInfoBean(1));
		List<String> listECode=new ArrayList<String>();
		for(int i=0;i<listEnterpriseBO.size();i++){
			listECode.add(listEnterpriseBO.get(i).getEcCode());
		}
		return listECode;
	}
}
